import { Component, OnInit, Input } from '@angular/core';
import { NzModalRef, NzMessageService } from 'ng-zorro-antd';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { UserService } from 'src/app/core/service/user.service';

@Component({
  selector: 'app-edit-admin-user',
  templateUrl: './edit-admin-user.component.html',
  styleUrls: ['./edit-admin-user.component.scss']
})
export class EditAdminUserComponent implements OnInit {
  @Input() email: string;
  @Input() firstName: string;
  @Input() lastName: string;
  @Input() userUuid: string;

  isChangePassword = 1;
  isSendActivationKey = 1;

  editAdminUserForm: FormGroup;
  info: any;
  isPasswordChange = false;     // 起初未选中，先隐藏
  isEmailChange = false;

  isHidden = true;              // 先隐藏
  isShow = false;

  msgFlag = '';

  constructor(
    private modal: NzModalRef,
    private message: NzMessageService,
    private userService: UserService,
    fb: FormBuilder
  ) {
    this.editAdminUserForm = fb.group({
      email: ['', Validators.compose([Validators.required, Validators.email])],
      lastName: ['', Validators.compose([Validators.required])],
      firstName: ['', Validators.compose([Validators.required])],
      newPassword: [''],
      confirmPwd: [''],
      isPassword: [''],
      isEmail: ['']
    });
  }

  ngOnInit() {
    this.editAdminUserForm.patchValue({
      email: this.email,
      firstName: this.firstName,
      lastName: this.lastName
    });
  }

  changeAdminUserModal() {
    console.log('提交', this.editAdminUserForm.controls);
    // tslint:disable-next-line:forin
    for (const c in this.editAdminUserForm.controls) {
      this.editAdminUserForm.controls[c].markAsTouched();
    }

    if (this.editAdminUserForm.valid) {
      if (this.isChangePassword === 0 && this.isSendActivationKey === 1) {
        // 要修改密码
        this.info = {
          email: this.editAdminUserForm.controls.email.value,
          lastName: this.editAdminUserForm.controls.lastName.value,
          firstName: this.editAdminUserForm.controls.firstName.value,
          isChangePassword: this.isChangePassword,
          isSendActivationKey: this.isSendActivationKey,
          passWord: this.editAdminUserForm.controls.newPassword.value,
          userUuid: this.userUuid
        };
      }

      if (this.isChangePassword === 0 && this.isSendActivationKey === 0) {
        // 要修改密码，并且要用邮件修改
        this.info = {
          email: this.editAdminUserForm.controls.email.value,
          lastName: this.editAdminUserForm.controls.lastName.value,
          firstName: this.editAdminUserForm.controls.firstName.value,
          isChangePassword: this.isChangePassword,
          isSendActivationKey: this.isSendActivationKey,
          userUuid: this.userUuid
        };
      }

      if (this.isChangePassword === 1) {
        //  不修改密码
        this.info = {
          email: this.editAdminUserForm.controls.email.value,
          lastName: this.editAdminUserForm.controls.lastName.value,
          firstName: this.editAdminUserForm.controls.firstName.value,
          isChangePassword: this.isChangePassword,
          userUuid: this.userUuid
        };
      }

      this.editService(this.info, '修改');
    } else {
      this.message.error('表单校验失败', { nzDuration: 10000 });
    }
  }

  private editService(info: any, msg: string) {
    this.userService.updateAdminUser(info)
      .subscribe((updateAdminUser) => {
        if (updateAdminUser.status) {
          this.message.success(msg + '成功!', {
            nzDuration: 10000
          });
          this.modal.destroy();
        } else {
          this.message.error(msg + '失败!请检查后重新填写', {
            nzDuration: 10000
          });
        }
      });
  }

  destroyModal(): void {
    this.modal.destroy({ data: 'this the result data' });
  }

  /**
   * 判断是否修改密码
   * @param value 有值表示被选中，无值表示未被选中
   */
  passwordChange(value: string[]) {
    if (value.length > 0) {       // 被选中
      this.isHidden = false;      // 被选中，显示
      this.isChangePassword = 0;  // 修改密码
    } else {  // 未选中
      this.isHidden = true;       // 未选中，隐藏
      this.isChangePassword = 1;  // 不修改密码
    }
  }

  emailChange(value: string[]) {
    if (value.length > 0) {       // 被选中
      this.isShow = true;
      this.isSendActivationKey = 0;
    } else {                      // 未选中
      this.isShow = false;
      this.isSendActivationKey = 1;
    }
  }

  /**
   * 判断邮箱是否重复
   */
  isEmailRepeat() {
    console.log('=== Janine.isEmailRepeat().email ===>', this.editAdminUserForm.controls.email.value);
    if (this.editAdminUserForm.controls.email.value) {
      this.userService.isEmailRepeat(this.editAdminUserForm.controls.email.value)
        .subscribe((data) => {
          console.log(data);
          if (data.status === 3) { // 重复
            this.msgFlag = 'error';
          }
          if (data.status === 1) { // 可用
            this.msgFlag = 'success';
          }
        });
    }
  }
}
